<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->database();
		$this->load->model('homeModel','CurModel');
		$this->load->model('User/SubjectModel','SubjectModel');
	}	
	
	function index(){
		
		
		$arr = array(
			'a'=>1
		);
		
		print_r($arr);
	}

	//更新balance
	function update_balance(){
		$date_month = $this->input->get('date_month',null,'');

		//获取上月期末结余
		$last_month = date("Y-m",strtotime("-1 months",strtotime($date_month)));
		$cash_res = $this->CurModel->get_cash_balance($last_month);
		$prev_money_0 = $prev_money_1 = $prev_money_2 = $cash_res['money'];

		if($date_month){
			$arr = array('date_month'=>$date_month);
		}

		$list = $this->CurModel->get_all($arr);
		if($list){
			//$arra = array();
			foreach($list as $item){
				$balance = 0;
				if($item['mtype']){//mtype=1为支出
					
					if($item['type'] == 1){//type=1为银行
						$prev_money_1 -= $item['money'];
						$balance = $prev_money_1;
					}else if($item['type'] == 2){//type=2为律所代收
						$prev_money_2 -= $item['money'];
						$balance = $prev_money_2;
					}else{//type=0为现金
						$prev_money_0 -= $item['money'];
						$balance = $prev_money_0;
					}
					
				}else{//mtype=0为收入
					
					//$prev_money += $item['money'];
					if($item['type'] == 1){//type=1为银行
						$prev_money_1 += $item['money'];
						$balance = $prev_money_1;
					}else{//type=0为现金
						$prev_money_0 += $item['money'];
						$balance = $prev_money_0;
					}

				}
				$this->CurModel->edit(array('balance'=>$balance),"id='{$item['id']}'");
				
			}
		}

	}

}
